/**
 * Created by yuris on 02.02.15.
 */
package InterfaceObjects.BottomPanel {
import InterfaceObjects.ButtonLong;

import feathers.controls.Label;
import feathers.text.BitmapFontTextFormat;

import starling.display.Image;
import starling.display.Sprite;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
import starling.filters.ColorMatrixFilter;

public class SocialItem extends Sprite {
    private var _skinImage:Image;
    private var _avatarImage:Image = null;
    private var _id:String;
    private var _topLabel:Label;
    private var _giftButton:ButtonLong;

    public function SocialItem(id:String, name:String, installed:String) {
        super();

        this._id = id;
        this._skinImage = GraphicFactory.getImageByName("Interface/Skins/bottom_panel_item_skin");
        this._skinImage.alignPivot();
        this._skinImage.width = 90;
        this._skinImage.height = 91;
        this.addChild(this._skinImage);

        this._topLabel = new Label();

        this._topLabel.textRendererProperties.useSeparateBatch = false; //use separate if text color will change
        this._topLabel.textRendererProperties.textFormat = new BitmapFontTextFormat("ComicDB_Dark",14,16777215,"left")//, 0x333333);//color change causes extra Draw Call
        this._topLabel.alignPivot();
        this._topLabel.maxWidth = 80;
        this._topLabel.y = - 65;
        this._topLabel.x = - name.length*3.5;
        this._topLabel.touchable = false;
        this._topLabel.text = name;
        addChild(this._topLabel);

        this._giftButton = new ButtonLong(ButtonLong.GREEN_TYPE);
        this._giftButton.width = 80;
        this._giftButton.height = 35;
        this._giftButton.y = 35;
        this._giftButton.x = 0;
        this._giftButton.alignPivot();
        addChild(this._giftButton);
        this._giftButton.visible = !installed;

        this.addEventListener(TouchEvent.TOUCH, HANDLER_touch);
    }

    private var _bufTouch:Touch;
    private function HANDLER_touch(event:TouchEvent):void {
        if(this._giftButton.visible){
            return;
        }

        this._bufTouch = event.getTouch(this);
        if(this._bufTouch){
            switch(this._bufTouch.phase){
                case TouchPhase.HOVER:
                    this._skinImage.filter = BottomPanel.itemHoverFilter;
                    break;
                case TouchPhase.BEGAN:
                    this._skinImage.filter = null;
                    break;
                case TouchPhase.ENDED:
                        //trace("Touch detected");
                    break;
            }
        } else {
            this._skinImage.filter = null;
        }
    }

    public function setAvatarImage(image:Image):void {
        this._avatarImage = image;
        this._avatarImage.alignPivot();
        this._avatarImage.touchable = false;
        //addChild(this._avatarImage);
    }

    public function showAvatar():void {
        if(this.parent && this._avatarImage){
            this._avatarImage.x = this.x + this._skinImage.width/2;
            this._avatarImage.y = this.y + this.height/2;
            this.parent.addChild(this._avatarImage);
        }
    }

    public function get avatarImage():Image {
        //TODO:if null return default silhouette
        return _avatarImage;
    }

    public function get id():String {
        return _id;
    }

    public function get giftButton():ButtonLong {
        return _giftButton;
    }
}
}
